package com.example.yskjproject.view.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.yskjproject.R;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LoginModel;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.utils.EventBusUtil;
import com.example.yskjproject.utils.KeyboardUtils;
import com.example.yskjproject.utils.SpCashUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * LoginWxDialogHelper
 * <p>
 * 作者：zx
 * 创建时间：2024/4/23
 */
public class SettingDialogHelper extends Dialog {


    private AppCompatTextView bd_phone,phone_code_tv,title_context;
    private AppCompatButton gh_phone, close_phone, phone_ok;

    private String phone;

    LinearLayout update_phone_one, update_phone_two;

    AppCompatImageView back_img,img_colose;

    AppCompatEditText ed_phone, ed_code;

    private CountDownTimer countDownTimer;

    public SettingDialogHelper(@NonNull Context context, String Phone) {
        super(context);
        setContentView(R.layout.setting_dialog);
        this.phone = Phone;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8); // 设置宽度为500dp
            layoutParams.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels * 0.28); // 设置高度为300dp
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setAttributes(layoutParams);
        }
        bd_phone = findViewById(R.id.bd_phone);
        bd_phone.setText(phone);
        gh_phone = findViewById(R.id.gh_phone);
        img_colose =findViewById(R.id.img_colose);
        img_colose.setOnClickListener(close ->{
            dismiss();
        });
        title_context = findViewById(R.id.title_context);
        update_phone_one = findViewById(R.id.update_phone_one);
        update_phone_two = findViewById(R.id.update_phone_two);
        back_img = findViewById(R.id.back_img);
        back_img.setOnClickListener(nextOne -> {
            update_phone_one.setVisibility(View.VISIBLE);
            update_phone_two.setVisibility(View.GONE);
            back_img.setVisibility(View.GONE);
            title_context.setText("手机号绑定设置");
        });

        gh_phone.setOnClickListener(GHPhone -> {
            update_phone_one.setVisibility(View.GONE);
            update_phone_two.setVisibility(View.VISIBLE);
            back_img.setVisibility(View.VISIBLE);
            title_context.setText("更换手机号绑定");
        });
        close_phone = findViewById(R.id.close_phone);
        phone_ok = findViewById(R.id.phone_ok);
        ed_phone = findViewById(R.id.ed_phone);
        ed_code = findViewById(R.id.ed_code);
        phone_code_tv =findViewById(R.id.phone_code_tv);
        phone_code_tv.setOnClickListener( code ->{
            KeyboardUtils.hideSoftInput(phone_code_tv);
            String Phone = ed_phone.getText().toString().trim();
            if (TextUtils.isEmpty(Phone)) {
                ToastUtil.toastShortMessage("请输入手机号!");
                return;
            }
            if (Phone.length() < 10) {
                ToastUtil.toastShortMessage("请输入正确的手机号!");
                return;
            }
            new Thread(() -> OkGo.<LzyResponse>get(Urls.YZM_CODE + "?mobile=" + Phone + "&type=" + 1)
                    .execute(new JsonCallback<LzyResponse>() {
                        @Override
                        public void onSuccess(Response<LzyResponse> response) {
                            if (TextUtils.equals("ok", response.body().msg)) {
                                Toasty.success(getContext(), "获取手机验证码成功!", Toast.LENGTH_SHORT, true).show();
                                countDownTimer = new CountDownTimer(60000, 1000) {
                                    @SuppressLint("SetTextI18n")
                                    public void onTick(long millisUntilFinished) {
                                        phone_code_tv.setText(millisUntilFinished / 1000 + "s");
                                    }

                                    public void onFinish() {
                                        phone_code_tv.setText("获取验证码");
                                    }
                                }.start();

                            } else {
                                ToastUtil.toastShortMessage("获取手机验证码失败!");
                            }
                        }

                        @Override
                        public void onError(Response<LzyResponse> response) {
                            super.onError(response);
                        }
                    })).start();
        });
        phone_ok.setOnClickListener(nextPhone -> {
            KeyboardUtils.hideSoftInput(ed_phone);
            KeyboardUtils.hideSoftInput(ed_code);
            String Phone = ed_phone.getText().toString().trim();
            String PhoneCode = ed_code.getText().toString().trim();
            if (TextUtils.isEmpty(Phone)) {
                ToastUtil.toastShortMessage("请输入手机号!");
                return;
            }
            if (Phone.length() < 10) {
                ToastUtil.toastShortMessage("请输入正确的手机号!");
                return;
            }
            if (TextUtils.isEmpty(PhoneCode)) {
                ToastUtil.toastShortMessage("请输入验证码!");
                return;
            }
            EventBusUtil.sendEvent(new EventMessage(EventContent.upDate_phone, Phone));
            dismiss();
        });
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}
